<template>
	<view>
		<u-toast ref="uToast" />
		<view class="top_box">
			<view class="collect" @click="collect">
				<image v-show="isCollect" src="/static/icons/collected.png" mode=""></image>
				<image v-show="!isCollect" src="/static/icons/collect.png" mode=""></image>
			</view>
			<view class="share">
				<image src="/static/icons/share.png" mode=""></image>
			</view>
		</view>
		<view class="video_box">

			<video id="myVideo" :src="cookDetail.vid" @play="play" object-fit="fill" @error="videoErrorCallback"
				:danmu-list="danmuList" enable-danmu danmu-btn controls :poster="cookDetail.coverpic"></video>
			<view class="wrap">
				<view class="content">
					<view>
						<view>
							<view>弹幕内容</view>
						</view>
						<view>
							<input v-model="danmuValue" type="text" placeholder="在此处输入弹幕内容" />
						</view>
					</view>
				</view>
				<button class="btn" @click="sendDanmu" size="mini">发送弹幕</button>
			</view>
		</view>

		<!-- 会员按钮 -->
		<view class="btn_wrap">
			<button v-if="!userInfo.vip" class="vip_btn" @click="open">会员低至2元/月，免广告、看100000+名厨视频</button>
			<view class="title">
				{{cookDetail.name}}
			</view>
			<view class="des">
				<view class="des_item" v-if="cookDetail.isFree == 'N'">
					<text>会员专享</text>
					<image src="/static/icons/vip.png"></image>
				</view>
				<view class="des_item">
					<text>{{cookDetail.pageview}}万浏览</text>
					<image src="/static/icons/watch.png"></image>
				</view>
				<view class="des_item">
					<text>{{cookDetail.collections}}收藏</text>
					<image src="/static/icons/collection.png"></image>
				</view>
			</view>
		</view>

		<!-- 制作详情 -->
		<view class="details">
			<view class="time">
				<view class="time_item">
					<image src="/static/icons/time.png" mode="widthFix"></image>
					<text>{{cookDetail.needtime}}分钟以上</text>
				</view>
				<view class="time_item">
					<image src="/static/icons/level.png" mode="widthFix"></image>
					<text>切墩（初级）</text>
				</view>
			</view>
			<view>
				<view class="ingredient_title">
					<view>用料</view>
					<view>移出采购清单</view>
				</view>
				<view class="ingredient_item" v-for="item in cookDetail.ingredient" :key="item">
					<view>{{item.name}}</view>
					<view>{{item.number}}</view>
				</view>
			</view>
			<view class="method">
				<view class="method_item" v-for="(item,index) in cookDetail.method" :key="item">
					<view class="step">
						步骤{{index+1}}
					</view>
					<image :src="item.img"></image>
					<view class="describe">
						{{item.describe}}
					</view>
				</view>
			</view>
			<view class="tips">
				<view class="tips_title">
					小贴士
				</view>
				<view class="tips_item" v-for="item in cookDetail.tips" :key="item">
					<view class="tipname">【{{item.tipname}}】</view>
					<view class="tipdescribe">
						<view v-for="(des,index) in item.tipdescribe" :key="des">
							{{index+1}}、{{des}}
						</view>

					</view>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: null,
				cookDetail: {},
				menuId: '',
				token: '',
				isCollect: false,
				danmuValue: '',
				danmuList: [{text: '奥利给',color: '#ff0000',time: 1},
					{text: '奥利给',color: '#bbffaa',time: 1},
					{text: '打工人打工魂，打工都是干饭人',color: '#bbffaa',time: 1},
					{text: '奥利给',color: '#bbffaa',time: 1},
					{text: '干饭人',color: '#bbffaa',time: 2},
					{text: '奥利给',color: '#bbffaa',time: 1},
					{text: '干饭人',color: '#bbffaa',time: 3},
					{text: '专业团队',color: '#bbffaa',time: 1},
					{text: '奥利给',color: '#bbffaa',time: 4},
					{text: '专业团队',color: '#bbffaa',time: 1},
					{text: '干饭人',color: '#ff00ff',time: 2},
					{text: '干饭人',color: '#ff00ff',time: 3},
					{text: '干饭人',color: '#ff00ff',time: 1},
					{text: '奥利给',color: '#ff00ff',time: 5},
					{text: '干饭人',color: '#ff00ff',time: 3},
					{text: '干饭人',color: '#ff00ff',time: 7},
					{text: 'yyds',color: '#ff00ff',time: 3},
					{text: '干饭人',color: '#ff00ff',time: 2},
					{text: '爷青回',color: '#ff00ff',time: 4},
					{text: '奥利给',color: '#ff00ff',time: 5},
					{text: '干饭人',color: '#ff00ff',time: 2},
					{text: '爷青回',color: '#ff00ff',time: 6},
					{text: '干饭人',color: '#ff00ff',time: 5},
					{text: '爷青回',color: '#ff00ff',time: 3},
					{text: '爷青回',color: '#ff00ff',time: 5},
					{text: 'yyds',color: '#ff0000',time: 9},
					{text: '干饭人',color: '#ff0000',time: 4},
					{text: 'yyds',color: '#ff0000',time: 9},
					{text: '奥利给',color: '#ff0000',time: 9},
					{text: 'yyds',color: '#ff0000',time: 6},
					{text: 'yyds',color: '#ff0000',time: 9},
					{text: '爷青回',color: '#ff0000',time: 8},
					{text: 'yyds',color: '#ff0000',time: 4},
					{text: 'yyds',color: '#ff0000',time: 9},
					
				],
			}
		},
		methods: {
			async getMenuDetail(_id) {
				const result = await this.$api.home.getMenuDetail({
					_id
				});
				if (result.meta.status == 200) {
					this.cookDetail = result.message[0];
					uni.setNavigationBarTitle({
						title: this.cookDetail.name
					})
				}
			},
			sendDanmu: function() {
				this.videoContext.sendDanmu({
					text: this.danmuValue,
					color: this.getRandomColor()
				});
				this.danmuValue = '';
			},
			videoErrorCallback: function(e) {
				uni.showModal({
					content: e.target.errMsg,
					showCancel: false
				})
			},
			play() {
				if (!this.userInfo) {
					this.videoContext.pause();
					this.$refs.uToast.show({
						title: '请先登录',
						type: 'warning',
						url: '/pages/login/login'
					})
				} else {
					if (!this.userInfo.isFree && !this.userInfo.vip) {
						this.videoContext.pause();
						this.$refs.uToast.show({
							title: '请先开通会员',
							type: 'warning',
						})
					}
				}
			},
			getRandomColor: function() {
				const rgb = []
				for (let i = 0; i < 3; ++i) {
					let color = Math.floor(Math.random() * 256).toString(16)
					color = color.length == 1 ? '0' + color : color
					rgb.push(color)
				}
				return '#' + rgb.join('')
			},
			// 开通会员
			open() {
				uni.navigateTo({
					url: '/pages/member/openMember'
				})
			},
			// 添加收藏
			async collect() {
				if (this.userInfo) {
					const result = await this.$api.profile.collect(this.token, {
						user_id: this.userInfo._id,
						menu_id: this.menuId
					});
					this.isCollect = !this.isCollect;
					this.getUserInfo();
					this.$refs.uToast.show({
						title: result.msg,
						type: 'success',
						duration: '2300'
					});
				} else {
					this.$refs.uToast.show({
						title: '请先登录',
						type: 'warning',
						url: '/pages/login/login'
					})
				}

			},

			// 获取最新用户数据
			async getUserInfo() {
				const result = await this.$api.profile.getUserInfo(this.token);
				this.userInfo = result[0];
				getApp().globalData.userInfo = result[0];

			}
		},
		onLoad: function(options) {
			this.getMenuDetail(options.id);
			this.menuId = options.id;
			uni.getStorage({
				key: 'token',
				success: ({
					data
				}) => {
					this.token = data;
				}
			})
		},
		onReady: function(res) {
			// #ifndef MP-ALIPAY
			this.videoContext = uni.createVideoContext('myVideo')
			// #endif
		},
		onShow: function() {
			this.userInfo = getApp().globalData.userInfo;
			// this.userInfo.vip = false
			if (this.userInfo) {
				this.isCollect = this.userInfo.mycollect.some(item => item == this.menuId);
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #f6f7f8;
	}

	.top_box {
		position: relative;
		display: flex;
		align-items: center;
		height: 50rpx;
		padding: 30rpx 0;
		background: #EE7B2D;

		>view {
			position: absolute;
			top: 0;
			width: 50rpx;
			height: 50rpx;

			>image {
				width: 100%;
				height: 100%;
			}
		}

		.collect {
			right: 120rpx;
		}

		.share {
			right: 50rpx;
		}
	}

	.video_box {
		width: 100%;
		background: #fff;

		#myVideo {
			width: 100%;
		}

		.wrap {
			display: flex;
			align-items: center;
			padding: 0 20rpx;

			.content {
				flex-grow: 1;
			}
		}
	}

	.btn_wrap {
		background: #fff;
		padding: 30rpx;

		.vip_btn {
			height: 80rpx;
			line-height: 80rpx;
			background: #E7B564;
			text-shadow: 0 0 100rpx #fff;
			font-size: 26rpx;
			color: #fff;
		}

		.title {
			margin: 30rpx 0;
			text-align: center;
			font-size: 32rpx;
		}

		.des {
			display: flex;
			justify-content: space-between;
			align-items: center;
			width: 500rpx;
			margin: 0 auto;

			.des_item {
				display: flex;
				align-items: center;

				>text {
					font-size: 24rpx;
					color: #999;
				}

				>image {
					margin-left: 10rpx;
					width: 30rpx;
					height: 30rpx;
				}
			}
		}
	}

	.details {
		margin-top: 30rpx;
		overflow: hidden;
		background: #FFFFFF;

		.time {
			display: flex;
			justify-content: space-between;
			width: 500rpx;
			margin: 50rpx auto;

			.time_item {
				display: flex;
				align-items: center;

				>image {
					width: 50rpx;
				}
			}
		}

		.ingredient_title {
			height: 80rpx;
			line-height: 80rpx;
			padding: 0 30rpx;
			border-bottom: 2rpx solid #ccc;
			display: flex;
			justify-content: space-between;

			>view {
				&:nth-child(2) {
					color: #ccc;
				}
			}
		}

		.method {
			width: 700rpx;
			margin: 0 auto;

			.method_item {
				width: 100%;
				margin: 20rpx 0;

				.step {
					margin: 10rpx 0;
					font-size: 30rpx;
					font-weight: 600;
				}

				>image {
					width: 100%;
					height: 400rpx;
				}

				.describe {
					color: #686868;
					width: 100%;
					margin: 10rpx 0;
				}
			}
		}

		.ingredient_item {
			height: 50rpx;
			line-height: 50rpx;
			border-bottom: 2rpx solid #ccc;
			padding: 0 30rpx;
			display: flex;

			.del {
				text-align: right;
			}

			>view {
				&:first-child {
					width: 500rpx;
				}
			}
		}

		.tips {
			padding: 0 30rpx;

			.tips_title {
				font-size: 36rpx;
				margin: 30rpx 0;
				color: #000;
				font-weight: 500;
			}

			.tips_item {
				.tipname {
					color: #666;
					font-size: 32rpx;
					font-weight: 500;
				}

				.tipdescribe {
					color: #999;
					margin: 20rpx 0;
					padding: 0 50rpx;
				}
			}
		}
	}
</style>
